package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.PowerChangeLog;
import com.tbit.main.pojo.PowerChangeLogSta;
import com.tbit.main.service.DispatchLogService;
import com.tbit.main.service.PowerChangeLogService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * 换电日志
 *
 * @author Leon
 * 2019年1月23日 下午3:51:50
 */
@RestController
@RequestMapping("/powerChangeLog")
public class PowerChangeLogController {
    @Autowired
    private PowerChangeLogService powerChangeLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private DispatchLogService dispatchLogService;

    /**
     * 分组统计
     */
    @RequestMapping("/groupByTime")
    public JsonResult groupByTime(String token, Integer accountId, String startTime, String endTime, Integer isValid,
                                  @RequestParam(value = "regionId", required = false) List<Integer> regionId,
                                  @RequestParam(value = "machineType", required = false) Integer machineType) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<PowerChangeLogSta> powerChangeLogStas = powerChangeLogService.groupByTime(accountId, accountUserToken.getAccountUserType(),
                accountUserToken.getAccountUserId(), startTime, endTime, regionId, machineType, isValid);

        return JsonResult.succ(powerChangeLogStas);
    }

    /**
     * 查询详细
     */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(Integer accountId, Integer userMainId, String startTime, String endTime, Integer isValid,
                                @RequestParam(value = "regionId", required = false) List<Integer> regionId,
                                @RequestParam(value = "machineType", required = false) Integer machineType,
                                String userCode, String batteryNO, String batteryNOOld) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<PowerChangeLog> powerChangeLogs = powerChangeLogService.getByTime(accountId, userMainId, startTime, endTime, regionId, machineType, isValid, userCode, batteryNO, batteryNOOld);

        return JsonResult.succ(powerChangeLogs);
    }

    /**
     * 判断车辆是否有效
     *
     * @param token
     * @param userCode
     * @return
     */
    @RequestMapping("/affirm")
    public JsonResult workOrderAffirm(String token, String userCode, Integer type) throws ParseException {

        /**换电*/
        if (type.equals(0)) {
            return JsonResult.succ(powerChangeLogService.workOrder(userCode));
        } else {
            return JsonResult.succ(dispatchLogService.workOrder(userCode));
        }
    }
}